package com.srxz.mobilepharmacy.service;

import com.srxz.mobilepharmacy.entity.UserInfo;
import com.srxz.mobilepharmacy.mapper.mapstruct.UserInfoMapper;
import com.srxz.mobilepharmacy.repository.UserInfoRepository;
import com.srxz.mobilepharmacy.service.mapstruct.UserInfoDTOService;
import com.srxz.mobilepharmacy.util.back.BackMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;

@Service
@Transactional
public class LoginService extends UserInfoDTOService {
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private UserInfoMapper userInfoMapper;

    public BackMessage login(BigInteger aid, String uname, String pwd, Integer house, HttpSession session){
        UserInfo userInfo = userInfoRepository.findUserInfoByName(uname, aid);
        if(userInfo == null){
            return BackMessage.error("用户不存在");
        }
        //TODO 验证密码是否正确--密码加密


        //TODO 用户写入Session
        session.setAttribute("user", userInfo.getId());
        session.setAttribute("userhouse", house);

        return BackMessage.success().putData("back", userInfoMapper.dto(userInfo));
    }


}
